package com.twd.allcomponent.shiro.config;

import com.twd.allcomponent.redis.utils.RedisUtils;
import com.twd.allcomponent.util.SnowflakeIdWorker;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionIdGenerator;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
//@Component
public class CustomSessionIdGenerator implements SessionIdGenerator, InitializingBean {
    @Autowired
    RedisUtils redisUtils;
    @Resource
    private RedisTemplate redisTemplate;
    @Override
    public Serializable generateId(Session session) {
        //... 生成id逻辑
//        ValueOperations valueOperations = redisTemplate.opsForValue();
//        Integer workerid = (Integer)valueOperations.get("worker", );
//        Long dataIncrId = redisUtils.incrBy("worker", 0);
        return new SnowflakeIdWorker(0,redisUtils.incrBy("dataId",1)).nextId();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        redisUtils.set("dataId",0);
    }
}
